export function buildRows(rows) {
  rows.forEach(row => {
    let records = row.processingRecordList
    if (!records && records.length == 0) {
      return
    }
    //正序
    records = records.reverse();
    records.forEach(record => {
      let workstation = record.workstation
      let processParameterList = toObj(record.processParameter)
      // list.push({ prop: workstation + 'processingTime', label: '下线时间', width: calculateWidth('下线时间') })
      // list.push({ prop: workstation + 'duration', label: '节拍/秒' })
      // list.push({ prop: workstation + 'processingResult', label: '工序结果', dist: 'processing_result' })

      row[workstation + '上线时间'] = record.beatTime
      row[workstation + '节拍'] = record.duration
      row[workstation + '工序结果'] = record.processingResult


      if (processParameterList == null) {
        return
      }

      processParameterList.forEach(parameter => {
        let title = parameter.name
        if (isFirstColumn(title)) {

          row[title] = parameter.value
          return
        }
        if (parameter.lower) {
          row[workstation + title + '下限'] = parameter.lower
        }
        if (parameter.status === 'upper') {
          row[workstation + title] = `<span class="upper" title="上限值:${parameter.upper} | 下限值:${parameter.lower}">${parameter.value}</span>`
        } else if (parameter.status === 'lower') {
          row[workstation + title] = `<span class="lower" title="上限值:${parameter.upper} | 下限值:${parameter.lower}">${parameter.value}</span>`
        } else {
          row[workstation + title] = `<span title="上限值:${parameter.upper} | 下限值:${parameter.lower}">${parameter.value}</span>`
        }
        if (parameter.upper) {
          row[workstation + title + '上限'] = parameter.upper
        }
      })

      row[workstation + '下线时间'] = record.processingTime
    })
  });
  return rows
}

let firstTitles = []

// let firstTitles = ['等级评估', '第八颗螺丝扭力', '第八颗螺丝角度']
function isFirstColumn(title) {
  return firstTitles.includes(title)
}

export function buildColumns(workstations) {
  let columns = []
  workstations.forEach(item => {
    const workstation = item.workstation
    columns.push({
      label: workstation + ' (' + item.deviceName + ')',
      prop: workstation,
      width: calculateWidth(workstation),
      children: buildChildren(workstation, item.parameterList)
    })
  })
  return columns
}


function buildChildren(workstation, params) {
  let list = []
  list.unshift({prop: workstation + '上线时间', label: '上线时间', width: calculateWidth('上线时间')})
  // list.unshift({ prop: workstation + 'beatTime', label: '上线时间', width: calculateWidth('上线时间') })
  params.forEach(item => {
    const businessName = item.businessName
    if (isFirstColumn(businessName)) {
      return
    }
    // if (item.lowerAddress) {
    //     const title = businessName + '下限'
    //     list.push({
    //         label: title,
    //         prop: workstation + title,
    //         width: calculateWidth(title)
    //     })
    // }
    list.push({
      label: businessName,
      prop: workstation + businessName,
      width: calculateWidth(businessName)
    })
    // if (item.upperAddress) {
    //     const title = businessName + '上限'
    //     list.push({
    //         label: title,
    //         prop: workstation + title,
    //         width: calculateWidth(title)
    //     })
    // }
  })
  // list.push({ prop: workstation + 'processingTime', label: '下线时间', width: calculateWidth('下线时间') })
  // list.push({ prop: workstation + 'duration', label: '节拍/秒' })
  // list.push({ prop: workstation + 'processingResult', label: '工序结果', dist: 'processing_result' })
  list.push({prop: workstation + '节拍', label: '节拍/秒'})
  list.push({prop: workstation + '工序结果', label: '工序结果', dist: 'processing_result'})
  list.push({prop: workstation + '下线时间', label: '下线时间', width: calculateWidth('下线时间')})
  return list
}


function toObj(json) {
  if (json == null || json == "") {
    return null;
  }
  return JSON.parse(json)
}


function calculateWidth(key) {
  if (key.indexOf('时间') != -1) {
    return 155;
  }
  let width = key.length * 20
  return width < 100 ? 100 : width
}
